﻿using NCP.Core;
using NCP.Model;
using NCP.Service;
using NCP.Util;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using WebApp.Filters;

namespace WebApp.Controllers
{
    [Authen]
    public class DeptMergeInfoController : Controller
    {
        private DeptMergeInfoService _service;

        public DeptMergeInfoController(DeptMergeInfoService service)
        {
            _service = service;
        }
        // GET: DeptMergeInfo
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add(string deptCode)
        {
            DeptMergeInfo item = new DeptMergeInfo();
            if (!string.IsNullOrEmpty(deptCode))
                item = _service.Get(deptCode);
            return View(item);
        }

        public ActionResult InitTransfer(string deptCode)
        {
            DeptMergeInfo item = new DeptMergeInfo();
            if (!string.IsNullOrEmpty(deptCode))
                item = _service.Get(deptCode);
            List<DeptInfo> deptlist = _service.GetAllDepts();
            string deptjsonstr = "[";
            foreach (var d in deptlist)
            {
                deptjsonstr += "{'value':'" + d.DeptCode + "','title':'" + d.DeptName + "'},";
            }
            deptjsonstr = deptjsonstr.TrimEnd(',');
            deptjsonstr += "]";
           

            List<DeptCompare> comparelist = _service.GetDeptCompares(deptCode);
            string checkeddatastr = "[";
            foreach (var c in comparelist)
            {
                checkeddatastr += "'" + c.DeptCode + "',";
            }
            checkeddatastr = checkeddatastr.TrimEnd(',');
            checkeddatastr += "]";
            Hashtable ht = new Hashtable();
            ht.Add("data", deptjsonstr);
            ht.Add("checkeddata", checkeddatastr);
            LayData ajaxRst = new LayData("0", "获取成功", ht);
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        //科室对照
        public ActionResult DeptCompare(string deptCode)
        {
            DeptMergeInfo item = new DeptMergeInfo();
            if (!string.IsNullOrEmpty(deptCode))
                item = _service.Get(deptCode);
            List<DeptInfo> deptlist = _service.GetAllDepts();
            string deptjsonstr = "[";
            foreach (var d in deptlist) {
                deptjsonstr += "{'value':'"+d.DeptCode+ "','title':'"+d.DeptName.Trim()+"'},";
            }
            deptjsonstr = deptjsonstr.TrimEnd(',');
            deptjsonstr += "]";
            ViewData["data"] = deptjsonstr;

            List<DeptCompare> comparelist = _service.GetDeptCompares(deptCode);
            string checkeddatastr = "[";
            foreach (var c in comparelist)
            {
                checkeddatastr += "'" + c.DeptCode + "',";
            }
            checkeddatastr = checkeddatastr.TrimEnd(',');
            checkeddatastr += "]";
            ViewData["checkeddata"] = checkeddatastr;
            return View(item);
        }

        public ActionResult Get(string deptCode)
        {
            DeptMergeInfo item = new DeptMergeInfo();
            if (!string.IsNullOrEmpty(deptCode))
                item = _service.Get(deptCode);
            LayData ajaxRst = new LayData("0", "获取成功", item);
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        public ActionResult Save(DeptMergeInfo item)
        {
            item.OperCode = MemberUtil.CurrentMember.Code;
            item.OperDate = DateTime.Now;
            int rst = _service.Save(item);
            LayData ajaxRst = new LayData("0", rst > 0 ? "保存成功" : "保存失败");
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        public ActionResult DelByCode(string deptCode)
        {
            int rst = _service.Del(deptCode);
            LayData ajaxRst = new LayData("0", rst > 0 ? "删除成功" : "删除失败");
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        //获取拼音首字母
        public ActionResult GetInitials(string deptName)
        {
            string initials = PublicUtil.Initials(deptName);
            LayData ajaxRst = new LayData("0","获取成功", initials);
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        //科室对照
        public ActionResult Compare()
        {
            string mergeCode = Request["mergeCode"];
            string depts = Request["depts"];
            string[] deptarry = depts.Split('|');
            List<DeptCompare> compareList = new List<DeptCompare>();
            foreach (string d in deptarry)
            {
                if (string.IsNullOrEmpty(d))
                    continue;
                DeptCompare item = new DeptCompare();
                item.DeptCode = d;
                item.MergeDeptCode = mergeCode;
                item.OperCode = MemberUtil.CurrentMember.Code;
                item.OperDate = DateTime.Now;
                compareList.Add(item);
            }
            int rst = _service.Compare(mergeCode, compareList);
            LayData ajaxRst = new LayData("0",rst>0? "保存成功":"保存失败");
            return Content(JsonHelp.SerializeObject(ajaxRst));
        }

        public ActionResult GetList()
        {
            Expressionable<DeptMergeInfo> exp = new Expressionable<DeptMergeInfo>();
            int pageIndex = 1;
            int pageSize = 20;
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                pageIndex = Convert.ToInt32(Request.Form["page"]);
            }
            if (!string.IsNullOrEmpty(Request.Form["limit"]))
            {
                pageSize = Convert.ToInt32(Request.Form["limit"]);
            }
            exp.AndIF(!string.IsNullOrEmpty(Request["DeptCode"]),t=>t.DeptCode.Contains(Request["DeptCode"]));
            exp.AndIF(!string.IsNullOrEmpty(Request["DeptName"]), t => t.DeptName.Contains(Request["DeptName"]));
            PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            List<DeptMergeInfo> dataList = _service.GetPageList(exp, pageModel);
            LayData ht = new LayData("0", "获取成功", dataList, pageModel.TotalCount);

            return Content(JsonConvert.SerializeObject(ht));
        }

        public ActionResult GetMissionDeptList(string gpCode,string drugCode)
        {
            List<MissionDept> dataList = _service.GetMissionDeptList(gpCode,drugCode);
            LayData ht = new LayData("0", "获取成功", dataList);

            return Content(JsonConvert.SerializeObject(ht));
        }
        //导出科室数据
        public ActionResult export()
        {
            DataTable dt = _service.export();
            string filename = ExcelHelper.ExportToExcel(dt, "科室数据");
            return File(filename, "application/vnd.ms-excel", "科室数据.xls");
        }
    }
}